String clazzTpl({package}) => """
import 'dart:convert';
import 'entity_factory.entity.dart';
import 'package:flutter/foundation.dart';

/// Generates Dart code
///支持的key
const codeKeyList = ['code'];
const msgKeyList = ['msg', 'message'];
const dataKeyList = ['', 'data', 'result'];

class BaseEntity<T> {
  dynamic code;
  String? message;
  dynamic data;

  BaseEntity({this.code, this.message, this.data});

  static String? _findCodeKey(json) {
    try {
      return codeKeyList.firstWhere((element) => json.containsKey(element));
    } catch (e) {
      return null;
    }
  }

  static String? _findMsgKey(json) {
    try {
      return msgKeyList.firstWhere((element) => json.containsKey(element));
    } catch (e) {
      return null;
    }
  }

  static String? _findDataKey(json) {
    if (json is Map) {
      try {
        return dataKeyList.firstWhere((element) => json.containsKey(element));
      } catch (e) {
        return null;
      }
    } else {
      //没有key，直接返回数组
      return '__Array__';
    }
  }

  BaseEntity.init(
      {required dynamic jsonData,
      required List<T?> mListData,
      required bool isRespList,
      required bool isDataList}) {
    if (isRespList) {
      this.data = mListData;
    } else {
      var code = jsonData[_findCodeKey(jsonData) ?? ''];
      var message = jsonData[_findMsgKey(jsonData) ?? ''];
      if (code != null) this.code = code;
      if (message != null) this.message = message;
      if (isDataList) {
        this.data = mListData;
      } else {
        if (mListData.isNotEmpty) this.data = mListData.first;
      }
    }
  }

  factory BaseEntity.fromJson(dynamic resp, String type) {
    dynamic jsonData;
    bool isRespList = false, isDataList = false;
    const baseTypes = [
      'int',
      'double',
      'String',
      'bool',
      'Uint8',
      'Uint8List',
      'Uint16',
      'Uint16List',
      'Uint32',
      'Uint32List',
      'Uint64',
      'Uint64List',
    ];
    if (baseTypes.contains(type)) {
      //泛型为基本类型
      return BaseEntity(data: resp);
    } else {
      if (resp is String) {
        try {
          jsonData = jsonDecode(resp);
        } catch (e) {
          return BaseEntity(data: resp);
        }
      } else if (resp is Map) {
        jsonData = resp;
      } else if (resp is List) {
        jsonData = resp;
      } else {
        return BaseEntity(data: resp);
      }
    }

    List<T> mListData = [];
    var dataKey = _findDataKey(jsonData);
    try {
      if (jsonData is Map && jsonData[dataKey] != null) {
        isRespList = false;
        if (jsonData[dataKey] is List) {
          isDataList = true;
          mListData = (jsonData[dataKey] as List).map<T>((v) {
            return EntityFactory.generateOBJ<T>(v);
          }).toList();
        } else {
          isDataList = false;
          mListData.add(EntityFactory.generateOBJ<T>(jsonData[dataKey]));
        }
      } else {
        //没有key，直接返回数组
        if (dataKey == '__Array__') {
          isRespList = true;
          if (jsonData is List) {
            mListData = jsonData.map<T>((v) {
              return EntityFactory.generateOBJ<T>(v);
            }).toList();
          }
        }
      }
    } catch (e) {
      if (kDebugMode) {
        print("【 EntityFactory Error !!! ====> \$e <==== 】");
      }
    }
    return BaseEntity.init(
        jsonData: jsonData,
        mListData: mListData,
        isRespList: isRespList,
        isDataList: isDataList);
  }
}
""";
